package com.geen.tuoke.ui.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.*
import com.geen.tuoke.bean.request.PhoneTaskRequest
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.toastShort

/**
 *
 * @Description: 任务搜索
 * @Author: geen
 * @CreateDate: 2022/1/19 23:10
 * @Version: 1.0
 */
class SearchTaskVm : BaseViewModel() {

    var currentTaskTag = MutableLiveData<TaskTag>()
    var queryContent = MutableLiveData<String>()
    var dataPage = MutableLiveData<DataPage<SearchTaskInfo>>()
    var customInfo = MutableLiveData<CustomerInfo>()
    var pageNo = 1
    var pageSize = 15

    var dataPageCustom = MutableLiveData<DataPage<CustomerInfo>>()

    fun taskQuery() {
        if (queryContent.value.isNullOrEmpty()) {
            return
        }
        val queryRequest =
            PhoneTaskRequest(queryContent.value.toString(), currentTaskTag.value!!.taskCode)
        request({ apiService.phoneTaskQuery(pageNo, pageSize, queryRequest) }, {
            dataPage.value = it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    /***
     * 查找顾客
     */
    fun customQuery() {
        if (queryContent.value.isNullOrEmpty()) {
            return
        }
        val queryRequest =
            QueryCustomerModel(currentTaskTag.value!!.taskCode, queryContent.value.toString())
        request({ apiService.getCustomerList(queryRequest, pageNo, pageSize) }, {
            dataPageCustom.value = it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    /***
     * 根据客户Id 获取客户信息
     */
    fun getCustomInfoById(customId: String) {
        request({ apiService.getCustomInfo(customId) }, {
            customInfo.value = it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    interface SearchTaskHandler {

        fun clickCancel(view: View)

    }

}